package com.orange.example.controller;

import com.orange.common.threadpool.factory.SelfDefinedNameThreadFactory;
import com.orange.common.web.trace.TraceIdConstant;
import com.orange.example.runableEvent.LogEvent;
import lombok.Value;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.util.Date;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Description: 该Controller为测试Controller，用于对模块进行测试
 */
@RestController
@Slf4j
public class TestController {
    @GetMapping("test")
    public String test(){
        return "Hello World";
    }

    @Resource(name="LogThreadPool")
    public ThreadPoolExecutor threadPoolExecutor;

    /**
     * @Description:测试链路追踪是否有效
     */
    @GetMapping("traceid")
    public String traceId(){
        log.info("记录日志01，当前线程的traceid为{}", MDC.get(TraceIdConstant.TRACE_ID));
        //10:58:12.149 9e2000f49acb4e4a9aabcd662a162c18 [http-nio-8080-exec-1] INFO  com.orange.example.controller.TestController - 记录日志01，当前线程的traceid为9e2000f49acb4e4a9aabcd662a162c18
        return "success";
    }


    /**
     * @Description:测试不使用异步日志时，打印10万条数据所用时间
     */
    @GetMapping("print1")
    public String print1(){
        long l = System.currentTimeMillis();
        for (int i=1;i<100000;i++){
            log.info("打印日志01{}",MDC.get(TraceIdConstant.TRACE_ID));
        }
        long l1 = System.currentTimeMillis();
        //
        System.out.println(l1-l);
        //1463ms，10万条日志需要1秒多，在不使用异步日志的情况下
        return "success";
    }
    /**
     * @Description:测试使用异步日志时，打印10万条数据所用时间
     */
    @GetMapping("print2")
    public String print2(){
        long l = System.currentTimeMillis();
        for (int i=1;i<100000;i++){
            log.info("打印日志01{}",MDC.get(TraceIdConstant.TRACE_ID));
        }
        long l1 = System.currentTimeMillis();
        //
        System.out.println(l1-l);
        //98ms,相当的快---目前是没有开启的
        return "success";
    }

    /**
     * @Description:测试线程池
     */
    @GetMapping("/testThreadPools")
    public String testThreadPool(){
       //注入日志业务线程池--[日志业务1-thread-1] INFO  com.orange.example.runableEvent.LogEvent - 日志任务执行
        threadPoolExecutor.execute(new LogEvent());
        return "success";
    }

}
